package com.jgsu.farm.controller;

import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.Video;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.VideoService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.FileUploadUtil;
import com.jgsu.farm.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @program: farm-system
 * @description: 视频资源
 * @author: Mr.zuo
 * @create: 2020-01-25 19:39
 **/
@Slf4j
@Api(value = "farm/sysuser/video", tags = "视频资源管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/video")
public class VideoController {

    @Autowired
    VideoService videoService;
    @Autowired
    FileUploadUtil fileUploadUtil;

    /**
     * 视频资源添加
     */
    @ApiOperation(value = "视频资源添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult questionAdd(String name, String format, String description,
                                    String author,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time, String location, String device,
                                    MultipartFile videofile) throws IOException {
        String uploadFile = fileUploadUtil.upload(videofile);
        Video video = new Video(name,format,description,author,time,location,device,uploadFile);
        try {
            videoService.add(video);
            log.info("视频资源添加成功，name为：{}", video.getName());
            return CommonResult.success(video);
        } catch (DataAddException e) {
            log.error("视频资源添加异常,name为：{}", video.getName());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "视频资源删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult videoDelete(@RequestParam("videoid") Integer videoid) {
        try {
            videoService.delete(videoid);
            log.info("删除id为：{}的视频资源信息",videoid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException d) {
            log.error("不存在id为：{}的视频资源信息，删除失败",videoid);
            throw new DataNotExistException(d.getCode(), d.getMessage());
        }
    }


    /**
     * 视频资源查询功能
     */
    @ApiOperation(value = "视频资源查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Video>> videoList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                         @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Video> videoList = videoService.findAll(page, pagesize);
            log.info("查询所有的视频资源");
            return CommonResult.success(videoList);
        } catch (DataNotExistException e) {
            log.error("视频资源表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 视频资源修改
     */
    @ApiOperation(value = "视频资源修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult videoUpdate(Integer videoid,String name, String format, String description,
                                    String author, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,
                                    String location, String device, MultipartFile videofile) throws IOException {
        String uploadFile = fileUploadUtil.upload(videofile);
        Video video = new Video(videoid,name, format, description, author, time, location, device, uploadFile);
        try {
            videoService.update(video);
            log.info("更新name为{}的视频资源成功", video.getName());
            return CommonResult.success(video);
        } catch (DataNotExistException e) {
            log.error("更新name为{}的视频资源失败", video.getName());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
